home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / MWCC03 / HOTKEY2.ZIP / HK2HOOK.PAS < prev    next >
Pascal/Delphi Source File  |  1993-08-18  |  2KB  |  81 lines

  1. {*** HK1Hook.dll
  2.  
  3.     This is the library that installs and uninstalls the wh_Keyboard System hook. It needs
  4.     to be in the same directory as the sample program 'Hotkey2.exe'. The library gets called
  5.     by the program directly so there is no import unit.
  6.  
  7. ***}
  8.  
  9. library HK1Hook;
  10.  
  11. {$C Fixed Preload Permanent}
  12.  
  13. uses WinTypes, WinProcs, Win31;
  14.  
  15. const
  16.  
  17.     HookInUse         : Boolean  = False;
  18.     HotKeyWnd         : HWnd     = 0 ;
  19.     wm_HotKey                    = wm_User + 1;
  20.     {*** far declaration for Callback function ***}
  21.     pToHotKeyHookProc : TFarProc = nil;
  22.  
  23. var
  24.  
  25.     HHotKeyHook : HHook;
  26.  
  27. {********** wh_Keyboard Callback Function **********}
  28.  
  29. function HotKeyHookProc(iCode: Integer; wParam: Word; lParam: LongInt): LongInt; Export;
  30. label
  31.     fExit;
  32. begin
  33.     if iCode < 0 then
  34.     begin
  35.         HotKeyHookProc := CallNextHookEx(HHotKeyHook, iCode, wParam, Longint(@lParam));
  36.         Exit;
  37.     end;
  38.     if (iCode = hc_Action) and (wParam <> 0) and (HotKeyWnd <> 0) then
  39.     begin
  40.         {*** Ignore key if it was released or if it's a repeat ***}
  41.         if (lParam = $80000000) or (lParam = $40000000) then
  42.             goto fExit;
  43.         {*** wParam contains the keys virtual key code and lParam can be used to get its name ***}
  44.         PostMessage(HotKeyWnd, wm_HotKey, wParam, lParam);
  45.     end;
  46.  
  47.     fExit:
  48.     HotKeyHookProc := CallNextHookEx(HHotKeyHook, iCode, WParam, Longint(@LParam));
  49. end;
  50.  
  51. {********** InstallHook **********}
  52.  
  53. procedure InstallHook(Wnd: HWnd); export;
  54. begin
  55.     if HookInUse then exit;
  56.     pToHotKeyHookProc := TFarProc(@HotKeyHookProc);
  57.     HHotKeyHook := SetWindowsHookEx(wh_Keyboard, THookProc(pToHotKeyHookProc), hInstance, 0);
  58.     HotKeyWnd := Wnd;
  59.     HookInUse := True;
  60. end;
  61.  
  62. {********** UnInstallHook **********}
  63.  
  64. procedure UnInstallHook; export;
  65. begin
  66.     if HookInUse = true then
  67.     begin
  68.         HookInUse := false;
  69.         UnhookWindowsHookEx(HHotKeyHook);
  70.         pToHotKeyHookProc := nil;
  71.         HotKeyWnd := 0;
  72.     end;
  73. end;
  74.  
  75. exports
  76.     InstallHook   index 1,
  77.     UnInstallHook index 2;
  78.  
  79. begin
  80. end.
  81.